<?php

$page_security = 'SA_SALESTRANSVIEW';
$path_to_root = "..";

include_once ($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
include_once ($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/includes/date_functions.inc");
include_once ($path_to_root . "/sales/includes/sales_db.inc");
include_once ($path_to_root . "/purchasing/includes/po_class.inc");
include_once ($path_to_root . "/pittycash/includes/db/pittycash_db.inc");
include_once ($path_to_root . "/admin/db/fiscalyears_db.inc");

$js = '';

if ($use_popup_windows)
{
    $js .= get_js_open_window(900, 500);
}

if ($use_date_picker) {
	$js .= get_js_date_picker();
}


//get_js_datetime_picker();
$Refs = new references();
page("Pembayaran Supplier");

if (!isset($_GET['order_no']) && !isset($_POST['order_no']))
{
    display_error('Halaman ini tidak bisa diakses langsung');
    exit;
}

if (isset($_GET['order_no']))
{
    $_POST['order_no'] = $_GET['order_no'];
    $ba = get_default_bank_account('IDR');
    $_POST['bank_id'] = $ba['id'];
}

$order_no = $_POST['order_no'];

simple_page_mode(true);

start_form();
hidden('order_no', $_POST['order_no']);
function can_process()
{
    $amount = input_num('amount');
    $total = input_num('total');
    $sisa = input_num('sisa');

    //if (is_first_pay(get_post('order_no')))
    //    {
    //        $default = $total / 2;
    //        if ($amount < $default)
    //        {
    //            $_POST['amount'] = $default;
    //            set_focus('amount');
    //            display_error('Pembayaran pertama harus 50% dari total.');
    //            return false;
    //        }
    //    }

    if ($amount > $sisa)
    {
        set_focus('amount');
        $_POST['amount'] = $sisa;
        display_error('Jumlah pembayaran salah.');
        return false;
    }

    return true;
}

//if(isset($_POST['SaveKartu']))
//{
//	close_production_card(get_post('wo_item'));
//    meta_forward($path_to_root,"application=manuf");
//}

if (isset($_GET['selected_id']))
{
    $selected_id = $_GET['selected_id'];
}

if ($Mode == 'ADD_ITEM' && can_process())
{
    save_supp_pay(get_post('ref'), get_post('no_kwitansi'), get_post('order_no'),
        get_post('date_t'), input_num('total'),input_num('total_pay'),
        input_num('amount'), get_post('branch_code'), get_post('bank_id'),
        get_post('note'));
        
    display_notification('Pembayaran Berhasil ditambahkan.');

    $Mode = 'RESET';
}

if ($Mode == 'Edit')
{
    if (get_post('editable'))
    {
        void_supp_pay($selected_id);
        display_notification('Pembayaran Berhasil divoid.');
    } else
    {
        display_notification('Pembayaran tidak bisa divoid karena sudah lunas.');
    }

    $Mode = 'RESET';
}

if ($Mode == 'RESET')
{
    $selected_id = -1;
    $wo = get_post('wo_item');
    unset($_POST);
    $_POST['wo_item'] = $wo;
    $_POST['bank_id'] = '1';
    // 	meta_forward($_SERVER['PHP_SELF'], "wo_item=$wo");
}


br();
//start_table(TABLESTYLE, "width=95%", 5);
start_outer_table(TABLESTYLE2, "width=60%", 5);

table_section(1);
//$woitems = get_sales_order_header($order_no, ST_SALESORDER);
//$order = new purch_order();
//read_po_header($order_no, $order);
$order = read_supp_trans($order_no,ST_PURCHORDER);
//read_po_items($order_no, $order);
//var_dump($order);
//exit;
//$woitems = db_fetch($result);
$result_pay = get_supp_pay($order_no);

$total = $order['total'];
//hidden('inv_order_no',$order->inv_order_no);
hidden('total', $total);
$total_pay = get_total_supp_pay($order_no);
hidden('total_pay',$total_pay);
$sisa = $total - $total_pay;
hidden('sisa', $sisa);
//hidden('branch_code', $woitems['branch_code']);
label_row("Supplier:", $order['supp_name']);
//label_row('Catatan:', $order->Comments);
label_row('Reference:', $order['reference']);
label_row("Total:", price_format($total));
table_section(2);
label_row("Tanggal Transaksi :", sql2date($order['ord_date']));
//label_row("Tanggal Dibutuhkan :", $order['due_date']);
//label_row("Tanggal Dibutuhkan :", sql2date($woitems['delivery_date']));
//$jmlpsn = get_sales_order_item_details_sum_pesanan($wo_id);
label_row("Sisa Pembayaran:", price_format($sisa));
end_outer_table();
//tabel bagian potong
br();
div_start('tables_');
start_table(TABLESTYLE, "width=60%");
$th = array(
    'No. Pembayaran',
    'No. Kwitansi',
    'Tanggal Bayar',
    'Jumlah',
    'Tipe',
    'Keterangan',
    '');
inactive_control_column($th);
table_header($th);
$k = 0;
//$result_pay = get_cust_pay($order_no);


while ($myrow = db_fetch($result_pay))
{
    submit_js_confirm("Edit" . $myrow["id"], "Anda yakin akan void Pembayaran?");
    alt_table_row_color($k);
    label_cell($myrow["ref"]);
    label_cell($myrow["no_kwitansi"]);
    label_cell(sql2date($myrow["date_"]));
    label_cell(price_format($myrow["amount"]));
    label_cell($myrow["tipe"]);    
    label_cell($myrow["note"]);
    if(is_date_in_fiscalyears(sql2date($myrow["date_"])))
        delete_button_cell("Edit" . $myrow["id"], 'Void Pembayaran');
    else
        label_cell('');
    end_row();
}

end_table();
div_end();
br();
if ($sisa > 0)
{
    div_start('details');
    start_table(TABLESTYLE2);
    //ref_row('No. Pembayaran' . ':', 'ref', _('Reference number unique for this document type'),
    //        $Refs->get_next(ST_PEMBAYARAN), '');
    $myref = $Refs->get_next(ST_PURCHPAY);
    label_row('No. Dokumen', $myref);
    hidden('ref', $myref);
    date_row('Tanggal Bayar:', 'date_t');
    text_row('No. Kwitansi:', 'no_kwitansi', '', 20, 25);
    //$dec = get_qty_dec(get_post('component'));
    //$_POST['quantity'] = number_format2(input_num('quantity', 1), $dec);
    bank_accounts_list_row('Bank:', 'bank_id', null, true);
    $bankbal = get_bank_balance(get_post('bank_id'));
    div_start('saldo_div');
    label_row('Saldo:', number_format($bankbal));
    hidden('saldo_bank',$bankbal);    
    div_end();
    amount_row("Jumlah:", 'amount');
    textarea_row('Catatan:', 'note', '', 20, 2);

    end_table(1);
    div_end();
    echo "<table align=center><tr><td>";

    submit_js_confirm('ADD_ITEM', "Anda yakin akan melakukan \nPEMBAYARAN?");
    submit('ADD_ITEM', "Simpan Pembayaran", true, '', true);

    echo "</td></tr></table>";
    hidden('editable', '1');

} else
{
    hidden('editable', '0');
}
br();

end_form();
end_page();

?>